package com.cincc.cincc_pbx.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.cincc.cincc_pbx.R;
import com.cincc.cincc_pbx.common.MyUtils;
import com.cincc.cincc_pbx.constant.CallConstant;
import com.cincc.cincc_pbx.model.EventBusModel;
import com.cincc.siphone.core.SipPhoneCtrl;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;


/**
 * 通话页面
 *
 * @author fengwawa
 */
public class CallActivity extends AppCompatActivity implements View.OnClickListener {

    TextView textCallNo;
    TextView textCallState;
    LinearLayout layoutInfo;

    LinearLayout layoutTool;

    Button btMute;
    Button btAnswer;
    Button btHandOff;
    Button btSpeaker;


    /**
     * 拨叫号码
     */
    private String callNumber;
    /**
     * 是否被叫接听
     */
    private boolean isCalledMode;
    /**
     * 加速通道时使用
     */
    private SipPhoneCtrl mSipPhoneCtrl = SipPhoneCtrl.Instance();
    private Handler _mHandler = new Handler();

    private boolean mMuted = false;
    private boolean mSpeaker = false;
    private static CallActivity instance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_landscape);
        ImmersionBar.with(this).statusBarDarkFont(false).init();
        instance = this;
        initView();
    }

    public static CallActivity GetInstance() {
        return instance;
    }

    public void finishCallPage() {
        this.finish();
    }

    public void connect() {
        second = 0;
        _mHandler.postDelayed(runnable, 1000);
    }

    /**
     * 设置视频和扬声器
     */
    public void setupCallView() {
        //默认免提
        _mHandler.postDelayed(() -> mSipPhoneCtrl.SetSpeakMode(), 500);
    }

    /**
     * 设置通话状态文本
     *
     * @param callText
     */
    public void setCallStateText(String callText) {
        textCallState.setText(callText);
    }

    int second = 0;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            second++;
            String time = MyUtils.convertSecondsToTime(second);
            textCallState.setText(time);
            _mHandler.postDelayed(this, 1000);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        instance = null;
        _mHandler.removeCallbacks(runnable);
//        _mHandler.postDelayed(() -> mSipPhoneCtrl.UnInitial(), 300);
//        _mHandler.postDelayed(() -> EventBus.getDefault().post(new EventBusModel(CallConstant.RE_REGISTER, null)), 1500);
    }

    /**
     * 相关按钮的事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_hand_off:
                mSipPhoneCtrl.Disconnect();
                finish();
                break;
            case R.id.bt_mute:
                mMuted = !mMuted;
                int muteRes = mMuted ? R.drawable.icon_mute_active : R.drawable.icon_mute_inactive;
                btMute.setBackgroundResource(muteRes);
                mSipPhoneCtrl.Mute(mMuted);
                break;
            case R.id.bt_speaker:
                mSpeaker = !mSpeaker;
                int speakerRes = mSpeaker ? R.drawable.icon_speakerphone_active : R.drawable.icon_speakerphone_inactive;
                btSpeaker.setBackgroundResource(speakerRes);
                mSipPhoneCtrl.SetSpeakMode();
                break;
            case R.id.bt_answer:
                mSipPhoneCtrl.Answer();
                break;
            default:
                break;
        }
    }

    /**
     * 初始化视图
     */
    private void initView() {
        textCallNo = findViewById(R.id.text_call_no);
        layoutInfo = findViewById(R.id.layout_info);
        layoutTool = findViewById(R.id.layout_tool);
        btMute = findViewById(R.id.bt_mute);
        btAnswer = findViewById(R.id.bt_answer);
        btHandOff = findViewById(R.id.bt_hand_off);
        btSpeaker = findViewById(R.id.bt_speaker);
        textCallState = findViewById(R.id.text_state);
        Intent intent = getIntent();
        callNumber = intent.getStringExtra("callNumber");
        isCalledMode = intent.getBooleanExtra("isCalledMode", false);

//        if (isCalledMode) {
//            layoutAnswer.setVisibility(View.VISIBLE);
//            textCallState.setText("来电，是否接听。。。。");
//        }

        textCallNo.setText(callNumber);

        //设置点击事件
        btMute.setOnClickListener(this);
        btHandOff.setOnClickListener(this);
        btSpeaker.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            onBackPressed();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    public void onBackPressed() {

    }
}